<template>
  <div>
    <div style="width: 100%;height: 70px;display:flex;background-color: white">
      <div style="width: 10%;">
        <img :src="`http://localhost:8089/upload/${course.brand}`" style="width: 100%;" >
      </div>
      <div style="width: 80%;display: flex">
        <img :src="`http://localhost:8089/upload/${course.cover}`" style="width: 88px;height: 50px;margin: 10px;border-radius: 6px">
        <div style="margin: 10px 0 0 10px">
          <a :href="'/#/courseView/'+courseId" style="font-weight:bold;font-size: 18px;color: #333333;margin: 0;text-decoration: none;">{{course.courseName}}</a>
          <p style="font-size: 14px;color: #00CC7E;margin-top:3px">{{course.userName}}</p>
        </div>
      </div>
      <div style="width: 10%;">
        <el-button style="border: 0;padding: 10px" @click="appraiseOpen()">
          <i class="el-icon-edit" style="font-size: 30px"></i>
          <p style="margin: 6px 0 0 0">评价课程</p>
        </el-button>
      </div>
    </div>
    <div style="display: flex;float: left;overflow: hidden">
      <el-menu :default-active="activeIndex" class="new-el-menu--sidebar el-menu-vertical-demo"
               @open="handleOpen" @close="handleClose" :collapse="isCollapse" router
               style="max-width:200px;height:300px;left:15px;top:15px;border-radius: 16px;padding: 10px 6px 10px 0">
        <div style="width: 30px;height: 30px;margin:5px 0 5px 18px;border: 2px solid #409EFF;border-radius: 8px">
          <i class="el-icon-arrow-right" @click="on_ro()"
             :style="isCollapse?'font-size: 22px;margin:4px 0 0 4px':'transform: rotate(180deg);font-size: 22px;margin:4px 0 0 4px'"></i>
        </div>
        <a style="margin-left:7px;font-size: 14px ">课程安排</a>
        <el-menu-item :index="'/learnEdit/announceEdit/'+course.courseId">
          <i class="emun-icon el-icon-bell "></i>
          <span slot="title" class="emun-span">公告</span>
        </el-menu-item>
        <el-menu-item :index="'/learnEdit/contentEdit/'+course.courseId">
          <i class="emun-icon el-icon-data-analysis"></i>
          <span slot="title" class="emun-span">课件</span>
        </el-menu-item>
        <el-menu-item :index="'/learnEdit/testListEdit/'+course.courseId">
          <i class="emun-icon el-icon-edit-outline"></i>
          <span slot="title" class="emun-span">测验与作业</span>
        </el-menu-item>
        <!--    <el-menu-item :index="'/learn/testList/'+'1'">-->
        <!--      <i class="emun-icon el-icon-document"></i>-->
        <!--      <span slot="title" class="emun-span">考试</span>-->
        <!--    </el-menu-item>-->
        <el-menu-item :index="'/learnEdit/forumindexEdit/'+course.courseId">
          <i class="emun-icon el-icon-chat-line-square"></i>
          <span slot="title" class="emun-span">讨论区</span>
        </el-menu-item>
<!--        <el-menu-item :index="'/learnEdit/testScoring/'+course.courseId">-->
<!--          <i class="emun-icon el-icon-document-checked"></i>-->
<!--          <span slot="title" class="emun-span">批改作业</span>-->
<!--        </el-menu-item>-->
      </el-menu>
      <router-view :key="$route.fullPath"/>
    </div>
    <div style="clear:both"></div>
    <el-dialog
        :visible.sync="appraiseVisible"
        width="30%"
        top="300px"
        center>
      <p style="width: 100%;font-size: 20px;color: #333333">学了这么久，说说你的学习感受吧！</p>
      <el-form :model="appraise" :rules="rules" style="width: 75%;margin-left: 65px" ref="appraiseForm">
        <el-form-item label="总评" prop="score">
          <el-rate
              v-model="appraise.score"
              :colors="colors"
              show-text>
          </el-rate>
        </el-form-item>
        <el-form-item label="评价内容" prop="comment">
          <el-input type="textarea" v-model:rows="appraise.comment" :autosize="{ minRows: 5}"
                    maxlength="100" show-word-limit></el-input>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button type="primary" style="width: 100px;margin-left: 188px" @click="onAppraise()">提交</el-button>
          <el-button style="width: 80px;margin-left: 10px" @click="appraiseVisible=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name:"LearnEditView",
  data(){
    return {
      courseId: 1,
      course: {},
      appraise: {},
      appraiseFlag:false,
      activeIndex: '',
      appraiseVisible: false,
      isCollapse: true,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      rules:{
        score:{required: true, message: '总评不能为空', trigger: 'blur'},
        comment:{required: true, message: '评价内容不能为空', trigger: 'blur'}
      }
    }
  },
  methods: {
    onAppraise(){
      this.$refs['appraiseForm'].validate((valid) => {
        if (valid) {
          if(this.appraiseFlag)
            this.addAppraise()
          else
            this.updateAppraise()
          setTimeout(() =>
                  this.updateScore()
              , 100)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    addAppraise(){
      this.$axios({
        method: 'post',
        url: "http://localhost:8089/appraise/updateAppraise",
        data:this.appraise
      }).then(res => {
        if(res.data.code===200){
          this.$message.success(res.data.msg);
          this.appraiseVisible=false;
        }else {
          this.$message.error(res.data.msg);
        }
      }).catch(err => console.log(err));
    },
    updateAppraise(){
      this.$axios({
        method: 'post',
        url: "http://localhost:8089/appraise/createAppraise",
        data:{"courseId":this.courseId,"userId":localStorage.getItem("userId"),
          "score":this.appraise.score,"comment":this.appraise.comment}
      }).then(res => {
        if(res.data.code===200){
          this.$message.success(res.data.msg);
          this.appraiseVisible=false;
        }else {
          this.$message.error(res.data.msg);
        }
      }).catch(err => console.log(err));
    },
    getAppraiseByCourseAndUser(){
      this.$axios({
        method: 'post',
        url: "http://localhost:8089/appraise/selectAppraiseByCourseAndUser",
        data:{"courseId":this.courseId,"userId":localStorage.getItem("userId")}
      }).then(res => {
        if(res.data.code===200){
          this.appraiseFlag=true;
          this.appraise = res.data.dataobject;
        }
      }).catch(err => console.log(err));
    },
    updateScore(){
      this.$axios({
        method: 'get',
        url: `http://localhost:8089/course/updateScore/${this.courseId}`
      }).then(res => {
      }).catch(err => console.log(err));
    },
    getCourseById(courseId) {
      this.$axios({
        method: 'get',
        url: `http://localhost:8089/course/selectCourseById/${courseId}`
      }).then(res => {
        this.course = res.data.dataobject;
      }).catch(err => console.log(err));
    },
    handleOpen(key, keyPath) {
      this.isCollapse=false;
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      this.isCollapse=true;
      console.log(key, keyPath);
    },
    on_ro(){
      if(this.isCollapse){
        this.handleOpen()
      }else {
        this.handleClose()
      }
    },
    appraiseOpen(){
      this.$nextTick(()=>{
        // 清空内容和校验
        this.$refs['appraiseForm'].resetFields()
        //   只清空校验
        //   this.$refs['appraiseForm'].clearValidate()
      })
      this.getAppraiseByCourseAndUser()
      this.appraiseVisible=true;
    },
    setCurrentRoute() {
      this.activeIndex = this.$route.path; // 通过他就可以监听到当前路由状态并激活当前菜单
    },
  },
  watch: {
    $route() {
      this.setCurrentRoute();
    },
  },
  created() {
    this.courseId=this.$route.params.courseId;
    this.setCurrentRoute();
    this.getCourseById(this.courseId);
    this.handleOpen();
    this.$nextTick(() => {
      this.handleClose();
    },1)
  }
}
</script>

<style scoped>
/deep/ .el-form-item__label{
  text-align:left;
  float: none;
  word-break: break-word;
}
/deep/ .el-rate__icon {
  font-size: 24px;
}
/deep/.el-dropdown-menu__item{
  padding: 0 !important;
}
/deep/.el-menu-item{
  height: 48px;
  margin: 10px 0 0 6px;
  border-radius: 16px;
}
.emun-icon{
  margin: 0 0 8px -4px;
  font-size: 26px;
  color: #409EFF
}
.emun-span{
//color: #333333;
  font-size: 16px;
  margin:0 0 10px 10px;
}
.el-menu-item.is-active {
  background: #409EFF !important;
  color: #fff!important;

  i {
    color: #fff !important;

  }
}
.new-el-menu--sidebar:not(.el-menu--collapse) {
  width: 220px;
}
</style>